﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Reflection;

namespace OTDR_OPT
{
    //连接仪表的配置参数
    public class ConnnectManagement
    {
        #region 单例模式
        private static ConnnectManagement connnectManagement = null;

        private ConnnectManagement() { }

        public static ConnnectManagement GetInstance()
        {
            if (connnectManagement == null)
            {
                connnectManagement = new ConnnectManagement();
            }
            return connnectManagement;
        }
        #endregion

        //配置文件保存的完整名称
        private string FileName = System.Environment.CurrentDirectory + "\\" + "ConnectConfig.xml";


        private void InitFile()
        {
            #region 如果文件不存在，添加文件
            if (!File.Exists(FileName))
            {
                XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-8", "yes"),
                    new XElement("Connections"));
                xDoc.Save(FileName);
            }
            #endregion
        }


        //保存连接配置,参数封装在对象的属性里传入
        public void SaveConnctConfig(string objName,object param)
        {
            #region 获取对象的所有属性
            Type type = param.GetType();
            PropertyInfo[] properties = type.GetProperties();
            #endregion
            InitFile();
            XElement root = XElement.Load(FileName);
            var paramResult = (from element in root.Elements("Connection")
                               where element.Attribute("Name") != null
                               && element.Attribute("Name").Value.Equals(objName)
                               select element
                               );
            #region 没有该产品的节点时，直接将所有属性添加到配置文件中
            if (paramResult.Count() == 0)
            {
                XElement element = new XElement("Connection",new XAttribute("Name",objName));
                root.Add(element);
                foreach (PropertyInfo property in properties)
                {
                    element.Add(new XElement(property.Name,property.GetValue(param)));
                }
                root.Save(FileName);
                return;
            }
            #endregion

            #region 存在该产品的节点时，对比传入参数与本地参数的差异来确定是否需要新增或更新值
            XElement paramElement = paramResult.SingleOrDefault();
            bool isChanged = false; //有没有发生变化，发生变化时才保存文件
            foreach(PropertyInfo property in properties)
            {
                string propertyName = property.Name; //属性名称
                string propertyValue = property.GetValue(param).ToString(); //属性值
                string localValue = GetObjParamValue(objName, propertyName); //属性的本地值
                #region 如果本地配置的值与传入的一致，则跳过本属性的值操作
                if (property.GetValue(param).ToString() == localValue)
                {
                    continue;
                }
                #endregion

                #region 如果本地配置文件返回值为空，则认为没有此内容，直接添加此内容
                if (localValue == "")
                {
                    paramElement.Add(new XElement(propertyName, propertyValue));
                    isChanged = true;
                    continue;
                }
                #endregion

                #region 本地值与传入值不一致，刷新本地值
                XElement currentPropertyElement = (from element in paramElement.Elements(propertyName)
                                                   select element).SingleOrDefault();
                currentPropertyElement.Value = propertyValue;
                isChanged = true;
                #endregion
            }
            if (isChanged) { root.Save(FileName); }
            #endregion
        }


        //获取指定对象的、指定属性值 
        public string GetObjParamValue(string objName,string propertyName) {
            if (!File.Exists(FileName)) { return ""; }
            XElement root = XElement.Load(FileName);
            var paramResult = (from element in root.Elements("Connection")
                               where element.Attribute("Name") != null
                               && element.Attribute("Name").Value.Equals(objName)
                               select element
                               );

            if (paramResult.Count() == 0)
            {
                return "";
            }
            XElement paramElement = paramResult.SingleOrDefault();
            foreach(XElement element in paramElement.Elements())
            {
                string elementName = element.Name.ToString();
                if (elementName == propertyName)
                {
                    return element.Value.ToString().Trim();
                }
            }
            return "";
        }


        //获取指定对象的所有属性名称及对应的值
        public Dictionary<string,string> GetObjAllParam(string objName) {
            if (!File.Exists(FileName))
            {
                return null;
            }
            XElement root = XElement.Load(FileName);
            var paramResult = (from element in root.Elements("Connection")
                               where element.Attribute("Name") != null
                               && element.Attribute("Name").Value.Equals(objName)
                               select element);
            if (paramResult.Count() == 0)
            {
                return null;
            }
            XElement paramElement = paramResult.SingleOrDefault();
            Dictionary<string, string> paramDic = new Dictionary<string, string>();
            foreach (XElement element in paramElement.Elements()) {
                #region 把每一个属性名及值放在字典内
                string name = element.Name.ToString();
                string value = element.Value.ToString();
                paramDic.Add(name,value);
                #endregion
            }
            return paramDic;
        }

    }
}
